package com.itheima.filter.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.filter.service.inter.StudentService;
import com.itheima.mapper.StudentMapper;
import com.itheima.project.PageBean;
import com.itheima.project.student.Student;
import com.itheima.project.student.StudentQueryParam;
import com.itheima.pojo.ProjectUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceIml implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Override
    public PageBean page(StudentQueryParam sqp) {
        //这两条语句需要紧挨在一起
        PageHelper.startPage(sqp.getPage(),sqp.getPageSize());
        Page<Student> students = studentMapper.page(sqp);

        List<Student> result = students.getResult();
        long total = students.getTotal();
        return new PageBean(total, result);
    }

    @Override
    public void deletById(List<Integer> ids) {

        studentMapper.deletById(ids);
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.saveStudent(student);
    }

    @Override
    public void modifyStudent(Student student) {
        ProjectUtil.checkSpace(student);
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.modifyStudent(student);
    }

    @Override
    public Student queryStudentById(Integer id) {

        Student student = studentMapper.queryStudentById(id);

        return student;
    }

    @Override
    public void Disciplinaryaction(Integer id, Integer score) {
        ProjectUtil.checkSpace(score);
        Integer oldScore = studentMapper.getScore(id);
        if((score+oldScore)<0){
            score = -oldScore;
        }
        studentMapper.Disciplinaryaction(id,score);
    }
}
